package com.jinghang.spark_base._010_RDD

import org.apache.spark.{SparkConf, SparkContext}

object _041Broadcast {

  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("").setMaster("local[1]")
    val sc = new SparkContext(conf)

    /**
      * 共享变量
      * 广播变量
      *
      */
    val data = Array(1, 2, 3, 4, 5)
    val distData = sc.parallelize(data)

    val broadcastVar = sc.broadcast(Array(1, 2, 3))
    val broadcastVar1 = sc.broadcast(List("aa", "2", 3))

    distData.foreachPartition(
      records => records.map {
        //每台节点在本地使用广播变量
        record => record + broadcastVar.value(0)
      }
    )

    println(broadcastVar.value(0))
    println(broadcastVar1.value(0))

  }

}
